package com.itheima.task;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.ALiYunUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

/**
 * @description: 定时器
 * @author: wangjingliang
 * @create: 2019-11-14 09:53
 **/
@Component
public class TaskDemo {
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void clearImp(){
        System.out.println("========删除时间:"+ new Date()+"=============");

        //根据redis中保存的两个set集合进行差值计算,获得垃圾图片名称的集合
        Set<String> sdiffs = redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).diff( RedisConstant.SETMEAL_PIC_DB_RESOURCES);


        for (String sdiff : sdiffs) {
            if (sdiff!=null||sdiff.trim()!=""){
                //删除阿里云上垃圾图片
                ALiYunUtils.deleteFile(sdiff);
            }
        }
        if (sdiffs.size()>0){
        //删除redis中是储存图片名
        redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).remove(sdiffs.toArray());
        }
    }
}